package com.yztc.xiami_app.ui.customletterindexview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.yztc.xiami_app.R;
import com.yztc.xiami_app.ui.receiveaddress.interfaces.IAddressSelectListener;

import java.util.List;

public class MySortAdapter extends BaseAdapter implements SectionIndexer {
	private List<UserModel> list = null;
	private LayoutInflater inflater = null;
	private IAddressSelectListener listener; //Item点击的监听器

	public MySortAdapter(Context context, List<UserModel> list,IAddressSelectListener listener) {
		this.list = list;
		this.listener = listener;
		inflater = (LayoutInflater) context
				.getSystemService(context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder mHolder;
		if (convertView == null) {
			mHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_province_list, parent,
					false);
			mHolder.textView_item_firstLetter = (TextView) convertView
					.findViewById(R.id.textView_item_firstletter);
			mHolder.textView_item_username = (TextView) convertView
					.findViewById(R.id.textView_item_username);

			convertView.setTag(mHolder);
		} else {
			mHolder = (ViewHolder) convertView.getTag();
		}
		mHolder.textView_item_username
				.setText(list.get(position).getUsername());
		// 每个字母分组中的首个position所对应的textView_item_firstLetter控件显示或隐藏
		int section = getSectionForPosition(position);
		if (position == getPositionForSection(section)) {
			mHolder.textView_item_firstLetter.setVisibility(View.VISIBLE);
			mHolder.textView_item_firstLetter.setText(list.get(position)
					.getFirstLetter());
		} else {
			mHolder.textView_item_firstLetter.setVisibility(View.GONE);
		}

		//省份列表的Item的点击事件
		mHolder.textView_item_username.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//通知UI来处理省份列表的Item的点击事件
				String username = list.get(position).getUsername();
				listener.provinceItemClick(username);
			}
		});


		return convertView;
	}

	class ViewHolder {
		private TextView textView_item_firstLetter;
		private TextView textView_item_username;
	}

	@Override
	public Object[] getSections() {
		// TODO Auto-generated method stub
		return null;
	}

	// 做字母索引的时候常常会用到SectionIndexer这个接口
	// 1. getSectionForPosition() 通过该item的position找该item在哪个字母分组
	// 2. getPositionForSection() 根据字母分组找这个组中第一个item的position
	@Override
	public int getPositionForSection(int sectionIndex) {
		for (int i = 0; i < getCount(); i++) {
			String firstLetter = list.get(i).getFirstLetter();
			int firstChar = firstLetter.charAt(0);
			if (firstChar == sectionIndex) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public int getSectionForPosition(int position) {
		return list.get(position).getFirstLetter().charAt(0);
	}

}
